package com.system.tree.entity.DTO;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import com.system.tree.entity.Tree;
public class TreeQueryDTO {
	private Long treeId;
	private String text;
	private String iconCls;
	private String viewType;
	private boolean leaf;
	private String fatherId;
	
	public Long getTreeId() {
		return treeId;
	}

	public void setTreeId(Long treeId) {
		this.treeId = treeId;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getIconCls() {
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public String getViewType() {
		return viewType;
	}

	public void setViewType(String viewType) {
		this.viewType = viewType;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public String getFatherId() {
		return fatherId;
	}

	public void setFatherId(String fatherId) {
		this.fatherId = fatherId;
	}

	public static Specification<Tree> getSpecification(TreeQueryDTO treeQueryDTO){
		Specification<Tree> spec = new Specification<Tree>(){
			@Override
			public Predicate toPredicate(Root<Tree> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				List<Predicate> list = new ArrayList<Predicate>();
				if(null !=treeQueryDTO &&!StringUtils.isEmpty(treeQueryDTO.getText())) {
					Predicate p1 = cb.like(root.get("text").as(String.class), "%"+treeQueryDTO.getText()+"%");
					list.add(p1);
				}
				if(null !=treeQueryDTO &&!StringUtils.isEmpty(treeQueryDTO.getIconCls())) {
					Predicate p1 = cb.like(root.get("iconCls").as(String.class), "%"+treeQueryDTO.getIconCls()+"%");
					list.add(p1);
				}
				if(null !=treeQueryDTO &&!StringUtils.isEmpty(treeQueryDTO.getViewType())) {
					Predicate p1 = cb.like(root.get("viewType").as(String.class), "%"+treeQueryDTO.getViewType()+"%");
					list.add(p1);
				}
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
	}
}
